<?php
/**
 * Created by PhpStorm.
 * User: BlueIdeaLirui
 * Date: 2019/09/04
 * Time: 14:58
 */

namespace business\elasticsearch\repositories;

use business\elasticsearch\models\LandConstructionModel;
use business\elasticsearch\models\LandProjectModel;
use business\elasticsearch\models\LandTakeModel;
use business\elasticsearch\repositories\EsQuery;

class LandDetailsRepository
{
    /**
     * 查询拿地数据记录
     * @param array $condition
     * @param array $include
     * @param array $aggregations
     * @param int $from
     * @param int $size
     * @param array $orderBy
     * @return array
     */
    public function queryLandTakeList
    (
        $condition = [],
        $include = [],
        $aggregations = [],
        $from = 0,
        $size = 0,
        $orderBy = ['take_land_date' => SORT_DESC]
    ) {
        $query = (new EsQuery())
            ->source($include)
            ->from(LandTakeModel::index(), LandTakeModel::type())
            ->query($condition);

        if (!empty($aggregations)) {
            foreach ($aggregations as $aggregation) {
                $query->addAggs($aggregation['name'], $aggregation['options']);
            }
        }

        $query->orderBy($orderBy)->offset($from)->limit($size);

        return [$query->count(), $query->createCommand()->search()];
    }

    /**
     * 查询开工数据记录
     * @param $condition
     * @param array $include
     * @param array $aggregations
     * @param int $from
     * @param int $size
     * @param array $orderBy
     * @return array
     */
    public function queryLandConstructionList(
        $condition,
        $include = [],
        $aggregations = [],
        $from = 0,
        $size = 0,
        $orderBy = ['issuing_time' => SORT_DESC]
    ) {
        $query = (new EsQuery())
            ->source($include)
            ->from(LandConstructionModel::index(), LandConstructionModel::type())
            ->query($condition);

        if (!empty($aggregations)) {
            foreach ($aggregations as $aggregation) {
                $query->addAggs($aggregation['name'], $aggregation['options']);
            }
        }

        $query->orderBy($orderBy)->offset($from)->limit($size);

        return [$query->count(), $query->createCommand()->search()];
    }

    /**
     * 查询项目数据记录
     * @param $condition
     * @param int $from
     * @param int $size
     * @return array
     */
    public function queryLandProjectList(
        $condition,
        $include = [],
        $aggregations = [],
        $from = 0,
        $size = 0,
        $orderBy = ['created_time' => SORT_DESC]
    ) {
        $query = (new EsQuery())
            ->source($include)
            ->from(LandProjectModel::index(), LandProjectModel::type())
            ->query($condition);

        if (!empty($aggregations)) {
            foreach ($aggregations as $aggregation) {
                $query->addAggs($aggregation['name'], $aggregation['options']);
            }
        }

        $query->orderBy($orderBy)->offset($from)->limit($size);

        return [$query->count(), $query->createCommand()->search()];
    }
}
